/**
 * 
 * @author Travis C lynch <travisclynch@student.midlandstech.edu>
 * @version Apr 17 2012
 * <p>
 * <b>Instructions:</b><br>
 * Write a program using the bucket sort idea to create histograms of an election.
 * <ul>
 * <li>The input data are in an external file named election.txt. The first line is an integer N representing the number of candidates. The next N lines are the names of the canditates. The remaining lines are individual votes, each recorded as an integer from 0 to N-1 representing the number of one of the candidates, as in election.txt, below.
 * <li>For input from election.txt, put the following in a try{} block, and import java.io.*; Scanner sc = new Scanner(new FileReader("election.txt"));
 * <li>If the Scanner is declared in a block, it cannot be referenced outside of the block.
 * <li>Remember, if sc is your Scanner, then you should always use sc.nextLine(); after each call to sc.nextInt() to clear the input buffer.
 * <li>You may assume that the input file is in the correct format.
 * </ul>
 * The output for the above file should look like:</br>
 * </br>
 * Election Results</br>
 * ================</br>
 * Albert  *****</br>
 * Ford    ***</br>
 * Harris  ******</br>
 * Jones   ********</br>
 * Smith   ************</br>
 * ================</br>
 * <p>
 * <b>Description:</b><br>
 * A class that uses Bucket sort algorithm and data import from a txt file
 * note* C:/Users/Zendoren/Workspace/HW12/bin/election.txt
 */
import java.io.*;
import java.util.*;
public class BucketSort {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] elections; //array to house votes
		String[] candidates; //array to house candidates name. array dim number corresponds to the dim number for the elections array. 
		int numberOfCandidate=0; //number of candidates read from 1st line of txt file being imported
		BufferedReader istream = null;
		String fname;
		Scanner kbd = new Scanner(System.in);
		System.out.println("Enter a filename: ");
		fname = kbd.nextLine();
		try {
			istream = new BufferedReader(new FileReader(fname));
		}
		catch (IOException e) {
			System.err.println(e.getMessage());
		}
		Scanner fin = new Scanner(istream);
		numberOfCandidate = fin.nextInt();
		fin.nextLine();
		elections = new int[numberOfCandidate]; //dim the size of the array for the # of candidates
		candidates = new String[numberOfCandidate]; //dim the size of the array for the # of candidates
		for(int i=0; i<numberOfCandidate; i++) candidates[i]=fin.nextLine(); //import the candidates names into the candidates array
		while(fin.hasNextInt()) elections[fin.nextInt()]++; //import votes into the elections array 
		fin.close(); 
		
		System.out.println("\nElection Results: \n=================="); //output the election results
		for(int i=0;i<numberOfCandidate;i++){
			System.out.print(candidates[i]);
			for(int j=0;j<elections[i];j++)	System.out.print("*");
			System.out.println();
		}
		System.out.print("==================");
	}

}
